/*      $Id: HTTPMessage.c 983 2009-09-01 02:45:42Z phrakt $    */
/*
 * Copyright (c) 2007 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <canopy/log.h>

#include <http/http.h>
#include <http/sock.h>

#include "mod_java.h"



#define NATIVE_MESSAGE_CLASS	"ca.terraquati.canopy.http.impl.HTTPMessageImpl"




static jstring HTTPMessage_getHeader (JNIEnv *, jclass, jstring);
static void    HTTPMessage_setHeader (JNIEnv *, jclass, jstring, jstring);


static const JNINativeMethod HTTPMessage_methods[] = {
	{
		"getHeader",
		"(Ljava/lang/String;)Ljava/lang/String;",
		HTTPMessage_getHeader
	},
	{
		"setHeader",
		"(Ljava/lang/String;Ljava/lang/String;)V",
		HTTPMessage_setHeader
	},
};

/*
 *
 *
 *
 */
int
mod_java_load_HTTPMessage(JNIEnv *env)
{
	jint ret;
	jclass class;

	if ((class = mod_java_getclass(env, NATIVE_MESSAGE_CLASS, 0)) == NULL)
		return (-1);

	ret = (*env)->RegisterNatives(env, class, HTTPMessage_methods,
	    sizeof(HTTPMessage_methods)/sizeof(HTTPMessage_methods[0]));
	if (ret != JNI_OK) {
		canopy_log(CNP_LOG_ERR, "failed to register native methods: %s",
		    mod_java_jnierr(ret));
		return (-1);
	}

	(*env)->DeleteLocalRef(env, class);

	return (0);
}

/*
 *
 *
 */
static jstring
HTTPMessage_getHeader(JNIEnv *env, jobject obj, jstring jname)
{
	char *name, *value;
	jstring jvalue = NULL;
	http_msg_t *msg = mod_java_getpeer(env, obj, "peer_hdl");

	if (msg == NULL) {
		/* throw an exception */
		return (NULL);
	}

	value = http_msg_gethdr(msg, name);
	if (value != NULL) {
		jvalue = mod_java_string_ctoj(env, value);
	}

	return (jvalue);
}

/*
 *
 *
 */
static void
HTTPMessage_setHeader(JNIEnv *env, jobject obj, jstring jname, jstring jvalue)
{
	int ret;
	char *name, *value;
	http_msg_t *msg = mod_java_getpeer(env, obj, "peer_hdl");

	ret = http_msg_sethdr(msg, name, value);
	if (ret == -1) {
	}
}
